package br.com.intelsys.basics.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.validator.GenericValidator;

public class Knife {
	private static final String INTERNAL_KEY = "#" + 0x412CA123 + 0x412CC123 + 0x111CA123;

	public static String getMD5() {
		return getMD5(Long.toString(System.currentTimeMillis()));
	}

	public static String getMD5(String value) {
		try {
			return new String(Hex.encodeHex(MessageDigest.getInstance("MD5").digest(value.getBytes())));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getMD5MaskedPrimaryKey(long value, String key, int size) {
		String md5 = getMD5(value + INTERNAL_KEY + key);
		if (size >= 16) {
			size = 8;
		}
		int multiply = getMD5MaskedMultiply(key);
		return md5.substring(0, size) + value * multiply + md5.substring(size, size * 2);
	}

	private static int getMD5MaskedMultiply(String key) {
		int multiply = key.length() % 3 + 3;
		if (multiply == 5) {
			multiply = 9;
		}
		return multiply;
	}

	public static int getMD5UmaskedPrimaryKey(String maskedValue, String key, int size) {
		try {
			int value = Integer.parseInt(maskedValue.substring(size, maskedValue.length() - size)) / getMD5MaskedMultiply(key);
			if (maskedValue.equals(getMD5MaskedPrimaryKey(value, key, size))) {
				return value;
			}
		} catch (Exception e) {

		}
		return -1;
	}

	public static String convertUTFToLatin1(String utfString) {
		if (utfString != null) {
			try {
				byte[] latin1 = utfString.getBytes("UTF-8");
				byte[] utf8 = new String(latin1, "ISO-8859-1").getBytes("UTF-8");
				return new String(utf8);
			} catch (UnsupportedEncodingException e) {
			}
		}
		return "";
	}

	public static int[] getCardinalNumbersInReverseArray(int number, int digtCount) {
		return getCardinalNumbersInReverseArray(Integer.toString(number), digtCount);
	}

	public static int[] getCardinalNumbersInReverseArray(long number, int digtCount) {
		return getCardinalNumbersInReverseArray(Long.toString(number), digtCount);
	}

	public static int[] getCardinalNumbersInReverseArray(String number, int digtCount) {
		int[] retArr = new int[digtCount];
		int j = digtCount - 1;
		for (int i = number.length(); i > 0; i--) {
			retArr[j--] = Integer.parseInt(Character.toString(number.charAt(i - 1)));
			if (j < 0) {
				break;
			}
		}
		return retArr;
	}

	public static String getEmailTruncated(String email) {
		if (email != null && GenericValidator.isEmail(email)) {
			String[] emailArr = email.split("@");
			return emailArr[0] + '@' + "...";
		}
		return null;
	}

	public static String implode(Object[] stringArr, char c) {
		return implode(stringArr, c, 0);
	}

	public static String implode(Object[] stringArr, char c, int start) {
		StringBuilder sb = new StringBuilder();
		for (int i = start; i < stringArr.length; i++) {
			if (sb.length() > 0) {
				sb.append(c);
			}
			sb.append(stringArr[i]);
		}
		return sb.toString();
	}

	public static String implode(Object[] stringArr, String s) {
		return implode(stringArr, s, 0);
	}

	public static String implode(Object[] stringArr, String s, int start) {
		StringBuilder sb = new StringBuilder();
		for (int i = start; i < stringArr.length; i++) {
			if (sb.length() > 0) {
				sb.append(s);
			}
			sb.append(stringArr[i].toString());
		}
		return sb.toString();
	}

	public static String implode(Collection<?> collection, char c) {
		return implode(collection.toArray(new Object[collection.size()]), c, 0);
	}

	public static String implode(Collection<?> collection, char c, int start) {
		return implode(collection.toArray(new Object[collection.size()]), c, start);
	}

	public static String implode(Collection<?> collection, String s) {
		return implode(collection.toArray(new Object[collection.size()]), s, 0);
	}

	public static String implode(Collection<?> collection, String s, int start) {
		return implode(collection.toArray(new Object[collection.size()]), s, start);
	}

	public static String[] explode(Object value, String separator) {
		if (value != null) {
			String valueStr = value.toString();
			if (valueStr.length() > 0) {
				return valueStr.split(separator);
			}
		}
		return new String[0];
	}
}
